package com.asialjim.wechat.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: ClassUtils ]</i></h2><strong>
 * <p> class utils
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/1/27   &nbsp;&nbsp; JDK 8
 */
@Slf4j
public class ClassUtils {

    public static String toNotNullString(Object object){
        Method[] methods = object.getClass().getMethods();
        final StringBuffer sb = new StringBuffer().append("WechatCallMessage:{");
        Arrays.stream(methods).filter(method -> method.getName().startsWith("get"))
                .forEach(method ->{
                    boolean accessible = method.isAccessible();
                    Object invoke = null;
                    try {
                        if (!accessible){ method.setAccessible(true);}
                        invoke = method.invoke(object);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        log.info("get method invoke exception:{}",e.getMessage());
                    } finally {
                        if (!accessible) {method.setAccessible(accessible);}
                    }
                    if (Objects.nonNull(invoke)){sb.append(",").append("\r\n\t").append(method.getName().replaceFirst("get","")).append(":").append(invoke); }
                });

        sb.append("\r\n}");
        return sb.toString().replaceFirst(",", "");
    }

    /**
     * <h4>METHOD DESCRIPTION</h4>
     * 获取字段值
     * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
     * @since 2021/1/28 12:02
     */
    public static Object getFieldValue(Object source, Field field){
        boolean accessible = field.isAccessible();
        try {
            if (!accessible) {field.setAccessible(true);}
            return field.get(source);
        } catch (IllegalAccessException e) {
            return null;
        } finally {
            field.setAccessible(accessible);
        }
    }

    public static boolean containsField(Class<?> targetClass,String fieldName){
        if (Objects.isNull(targetClass) || StringUtils.isBlank(fieldName)){return false;}

        Field[] fields = targetClass.getFields();
        if (Arrays.stream(fields).anyMatch(field -> field.getName().equals(fieldName))){
            return true;
        }

        Field[] declaredFields = targetClass.getDeclaredFields();
        if (Arrays.stream(declaredFields).anyMatch(field -> field.getName().equals(fieldName))){
            return true;
        }

        return false;
    }
}